import 'package:flutter/material.dart';
import 'package:getx_mvvm_lite/core/state/page_state.dart';
import 'package:getx_mvvm_lite/shared/widgets/empty_view.dart';
import 'package:getx_mvvm_lite/shared/widgets/error_view.dart';
import 'package:getx_mvvm_lite/shared/widgets/loading_view.dart';

class StateScaffold extends StatelessWidget {
  final PageState state;
  final String title;
  final Widget child;
  final VoidCallback? onRetry;

  const StateScaffold({
    super.key,
    required this.state,
    required this.title,
    required this.child,
    this.onRetry,
  });

  @override
  Widget build(BuildContext context) {
    Widget body;
    if (state.isLoading) {
      body = const LoadingView();
    } else if (state.isError) {
      body = ErrorView(message: state.message, onRetry: onRetry);
    } else if (state.isEmpty) {
      body = EmptyView(message: state.message);
    } else {
      body = child;
    }
    return Scaffold(appBar: AppBar(title: Text(title)), body: body);
  }
}